#回合制对战游戏
#创建角色：名字、性别、血量、装备
class Role():
    def __init__(self,name,gender,blood=100,equip=None):
        self.name = name
        self.gender = gender
        self.blood = blood
        self.equip = equip
        self.room = None

    def fight(self,other):
        if self.room == other.room:
            if self != other:
                if self.blood >0 and other.blood > 0:
                    reduce_blood = 5 if other.equip else 10
                    other.blood -= reduce_blood
                    print(f"{self.name}造成了{other.name}{reduce_blood}点的伤害\t{self.name}当前血量:{self.blood}")
                else:
                    print("有一方已经死亡")
                    exit()
            else:
                print("你不能攻击你自己")
        else:
            print("攻击对象，不在同一个房间")

class Room():
    def __init__(self,name):
        self.member = []
        self.name = name
    def join(self,role):
        if role.room == None:
            if len(self.member) <2:
                #member房间属性，房间只能加两个人
                self.member.append(role)
                #达到条件，定义角色加入到某个房间
                role.room = self.name
            else:
                print("此房间已经满员了")
        else:
            print("此角色已经加入房间了！")
role_dict = {}  #存放名字和对象的关系
room_dict = {}  #存放初始化实例
#创建房间
#角色可以加入房间
#房间内的人可以互打
#房间上限两人
while True:
    print("请选择：1.创建角色\t2.创建房间\t3.加入房间\t4.开始战斗\t5.退出")
    enter_number = input("请输入你的选择\t")
    if enter_number.isdigit():
        enter_number = int(enter_number)
        if 1<= enter_number <=5:
            if enter_number ==1:
                name,gender = input("请输入角色的姓名和性别,空格隔开\n").split()
                #split()是为了 分割将字符赋给两个变量
                role_dict[name] = Role(name,gender)
                def_blood = input("请输入自定义血量\n")
                if def_blood.isdigit():
                    role_dict[name].blood = int(def_blood)
                    print(f"自定义成功,用户{name}当前血量{role_dict[name].blood}")
                else:
                    print(f"自定义失败")
                wear_body_equip = input("输入y装备防弹衣\n")
                if wear_body_equip == "y":
                    role_dict[name].body_armor = 1
            elif enter_number == 2:
                name = input("请输入房间的名字\n")
                room_dict[name] = Room(name)
            #创建好的对象保存在字典里
            elif enter_number == 3:
                print(f"当前角色有{role_dict.keys()}，当前房间有{room_dict.keys()}")
                role_name, room_name = input("请输入要加入的角色和房间名，空格隔开\n").split()
                room_dict[room_name].join(role_dict[role_name])
                continue
            elif enter_number == 4:
                print(f"当前房间有{room_dict.keys()}")
                room_name = input("请输入房间名字\n")
                if len(room_dict[room_name].member) ==2:
                    while True:
                        room_dict[room_name].member[0].fight(room_dict[room_name].member[1])
                        room_dict[room_name].member[1].fight(room_dict[room_name].member[0])
                else:
                    print(f"房间{room_name}人数不足2人，对战失败！")
                    continue
            elif enter_number == 5:
                print("退出对战")
                break
            else:
                print("输入错误，请输入1-5选择操作！")
        else:
            print("输入错误，请输入1-5选择操作！")


